import { requestBus } from '@/utils/http/middleware'
import { CanceledError } from 'axios'
import { cloneDeep } from '@/utils/common'

export const repeatResponseSuccess = (response: any) => {
  const pendKey = response.config.pendKey
  if (pendKey && requestBus.event[pendKey]) {
    requestBus.emit(pendKey, cloneDeep(response), 'resolve')
    delete requestBus.event[pendKey]
  }
}

export const repeatResponseError = (error: any) => {
  const pendKey = error.config.pendKey
  if (error instanceof CanceledError && pendKey) {
    return new Promise((resolve, reject) => {
      requestBus.on(pendKey, resolve, reject)
    })
  }
  return Promise.reject(error)
}
